package gatewayapigrpcroute

/*
Copyright 2021-2025 The k8gb Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"testing"

	k8gbv1beta1 "github.com/k8gb-io/k8gb/api/v1beta1"
	"github.com/k8gb-io/k8gb/controllers/utils"
	"github.com/stretchr/testify/assert"
)

func TestGetServers(t *testing.T) {
	var tests = []struct {
		name            string
		grpcRouteFile   string
		expectedServers []*k8gbv1beta1.Server
	}{
		{
			name:          "single hostname and backend; backend without namespace specified",
			grpcRouteFile: "../testdata/gatewayapi_grpcroute.yaml",
			expectedServers: []*k8gbv1beta1.Server{
				{
					Host: "gatewayapi-grpcroute.cloud.example.com",
					Services: []*k8gbv1beta1.NamespacedName{
						{
							Name:      "gatewayapi-grpcroute-service",
							Namespace: "test-gslb",
						},
					},
				},
			},
		},
		{
			name:          "single hostname and backend; backend with namespace specified",
			grpcRouteFile: "./testdata/gatewayapi_grpcroute_backend_with_namespace.yaml",
			expectedServers: []*k8gbv1beta1.Server{
				{
					Host: "gatewayapi-grpcroute.cloud.example.com",
					Services: []*k8gbv1beta1.NamespacedName{
						{
							Name:      "gatewayapi-grpcroute-service",
							Namespace: "test-backend",
						},
					},
				},
			},
		},
		{
			name:          "multiple hostnames",
			grpcRouteFile: "./testdata/gatewayapi_grpcroute_multiple_hostnames.yaml",
			expectedServers: []*k8gbv1beta1.Server{
				{
					Host: "gatewayapi-grpcroute1.cloud.example.com",
					Services: []*k8gbv1beta1.NamespacedName{
						{
							Name:      "gatewayapi-grpcroute-service",
							Namespace: "test-gslb",
						},
					},
				},
				{
					Host: "gatewayapi-grpcroute2.cloud.example.com",
					Services: []*k8gbv1beta1.NamespacedName{
						{
							Name:      "gatewayapi-grpcroute-service",
							Namespace: "test-gslb",
						},
					},
				},
			},
		},
		{
			name:          "multiple backendRefs",
			grpcRouteFile: "./testdata/gatewayapi_grpcroute_multiple_backendrefs.yaml",
			expectedServers: []*k8gbv1beta1.Server{
				{
					Host: "gatewayapi-grpcroute.cloud.example.com",
					Services: []*k8gbv1beta1.NamespacedName{
						{
							Name:      "gatewayapi-grpcroute-service1",
							Namespace: "test-gslb",
						},
						{
							Name:      "gatewayapi-grpcroute-service2",
							Namespace: "test-gslb",
						},
					},
				},
			},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// arrange
			grpcRoute := utils.FileToGatewayAPIGRPCRoute(test.grpcRouteFile)
			resolver := ReferenceResolver{
				grpcRoute: grpcRoute,
			}

			// act
			servers, err := resolver.GetServers()
			assert.NoError(t, err)

			// assert
			assert.Equal(t, test.expectedServers, servers)
		})
	}
}
